﻿using System.Collections.Generic;
using System.Linq;
using TweetSayingsLibrary.App.DataDoc;
using Dimebrain.TweetSharp.Fluent;
using TweetSayingsLibrary.Xtensions;

namespace TweetSayingsLibrary.App.Entity
{
    public class Users
    {
        public static IDataDoc ShowProfileFor(string screenname)
        {                     
            return Current.SessionTrack.ApiCaller.Request(t => t.Users().ShowProfileFor(screenname.ToLower()), false);
        }

        public static IDataDoc GetFollowers(string screenname)
        {
            return Current.SessionTrack.ApiCaller.Request(t => t.Users().GetFollowers().For(screenname.ToLower()).CreateCursor(), true);
        }

		public static IDataDoc GetFriends(string screenname)
		{
			return Current.SessionTrack.ApiCaller.Request(t => t.Users().GetFriends().For(screenname.ToLower()).CreateCursor(), true);
		}

        public static string ScreenNameLike(string partMatch)
        {
            if (partMatch.StartsWith("@"))
                partMatch = partMatch.Substring(1);

        	partMatch = partMatch.ToLower();

            IEnumerable<string> ppl = Current.SessionTrack.ScreenNames;

            if (ppl == null || !ppl.Any())
                return string.Empty;

            string name = ppl.First();
            int closest = name.LevenshteinDistance(partMatch);

            foreach (string peep in ppl)
            {
                string peeppart = peep;
                if (peeppart.Length > partMatch.Length)
                    peeppart = peeppart.Substring(0, partMatch.Length);

                int ld = peeppart.ToLower().LevenshteinDistance(partMatch);
                if (ld < closest)
                {
                    closest = ld;
                    name = peep;
                }
            }

            return name;
        }
	}
}
